home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 12 / BBS in a box XII-2.iso / Files II / Prog / B-C / C++ FAQ Reference 1.0.sit / C++ FAQ Reference 1.0.rsrc / TEXT_1630.txt < prev    next >
Encoding:
Text File  |  1993-06-30  |  1.2 KB  |  23 lines

  1. Because a member function is meaningless without an object to invoke it on, you can't do this directly (if 'X' were rewritten in C++, it would probably pass references to *objects* around, not just pointers to fns; naturally the objects would embody the required function and probably a whole lot more).
  2.  
  3. As a patch for existing software, use a free function as a wrapper which takes an object obtained through some other technique (held in a global, perhaps) and calls the desired member function.  There is one exception: static member functions do not require an actual object to be invoked, and ptrs-to-static- member-fns are type compatible with regular ptrs-to-fns (see ARM p.25, 158).
  4.  
  5. Ex: suppose you want to call X::memfn() on interrupt:
  6.  
  7.     class X {
  8.     public:
  9.              void memfn();
  10.       static void staticmemfn();    //a static member fn can handle it
  11.       //...
  12.     };
  13.  
  14.     //wrapper fn remembers the object on which to invoke memfn in a static var:
  15.     static X* object_which_will_handle_signal;
  16.     void X_memfn_wrapper() { object_which_will_handle_signal.memfn(); }
  17.  
  18.     main()
  19.     {
  20.       /* signal(SIGINT, X::memfn); */   //Can NOT do this
  21.       signal(SIGINT, X_memfn_wrapper);  //Ok
  22.       signal(SIGINT, X::staticmemfn);   //Also Ok
  23.     }